<?php

/**
 * This is the model class for table "{{users}}".
 *
 * The followings are the available columns in table '{{users}}':
 * @property integer $id
 * @property string $last_visit
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $role
 * @property string $identity
 * @property string $network
 * @property string $full_name
 * @property string $sex
 * @property string $avatar
 * @property integer $disabled
 * @property integer $language
 * @property string $address
 * @property string $phone
 * @property integer $country_id
 * @property string $city_id
 * @property string $birthday
 * @property integer $love_status
 * @property string $status
 * @property string $about
 */
class Users extends BaseModel
{
	public $oldPassword;
	public $newPassword;
	public $confirmPassword;
	public $oldEmail;
	public $newEmail;
	public $searchName;

	public $allRoles = array(
		'user'		=>	'Пользователь',
		'moderator'	=>	'Модератор',
		'admin'		=>	'Администратор',
	);

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{users}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email', 'required'),
			array('disabled, language, country_id, language_profile,
				message_in,reservation_my,reservation_upcoming,reservation,review_my,reference,reference_request,review_guests, city_id', 
				'numerical', 'integerOnly'=>true),
			array('username, password, email', 'length', 'max'=>128),
			array('role, identity, network, full_name, avatar, address, phone, status', 'length', 'max'=>255),
			array('sex', 'length', 'max'=>1),
			array('about', 'length', 'max'=>1000),
			array('last_visit, birthday, oldPassword, newPassword, confirmPassword', 'safe'),
			array('newEmail', 'email'),
			array('oldPassword', 'unsafe'),
			array('oldPassword', 	'compare', 	'compareValue'=>$this->password, 'allowEmpty'=>true),
			array('newPassword', 	'compare', 	'compareAttribute'=>'confirmPassword'),
			array('oldEmail', 		'compare', 	'compareValue'=>$this->email, 'allowEmpty'=>true),
			// array('confirmPassword','compare',	'compareAttribute'=>'newPassword'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, last_visit, username, password, email, role, identity, network, full_name, sex, avatar, disabled, language, address, phone, country_id, city_id, birthday, status, about, language_profile', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			// Objects
		/*
			'objects'		=>	array(self::HAS_MANY, 'Object', 'user_id'),
			// Bookmarks
			'favoritesBkm'	=>	array(self::HAS_MANY, 'Favorites', 'user_id'),
			'bookmarks'		=>	array(self::HAS_MANY, 'Object', array('object_id'=>'id'),
				'through'	=>	'favoritesBkm',
				'select'	=> 'id, name, image_id, user_id, room_type_id, price',
				'limit' 	=> 	10,
				'order' 	=>	'RAND()',
			),
			// comments
			'comments'		=>	array(self::HAS_MANY, 'UsersComments', 'profile_id'),
			// rating_review
			'positive_review'	=>	array(self::STAT, 'UsersComments', 'profile_id',
				'condition'	=>	'rating_review=1'
			),
			'negative_review'	=>	array(self::STAT, 'UsersComments', 'profile_id',
				'condition'	=>	'rating_review=2'
			),
			*/
// old relations !!!!!
			// 'friends'=>array(self::HAS_MANY, 'Friends', 'user_id',
			// 	'condition'=>'friends.status=1',
			// 	'limit' => 6,
			// 	'order' => 'RAND()',
			// ),					
			// 'countFriends'=>array(self::STAT, 'Friends', 'user_id',
			// 	'condition'=>'status=1',
			// ),
			// 'isFriend'=>array(self::HAS_ONE, 'Friends', 'friend_id',
			// 	'condition'=>'isFriend.user_id=:user_id',
			// 	'params'=>array(':user_id'=>Yii::app()->user->id)
			// ),			
			// 'albums'=>array(self::HAS_MANY, 'Albums', 'user_id',
			// 	'order' => 'RAND()',
			// ),
			// 'countAlbums'=>array(self::STAT, 'Albums', 'user_id'),	
			// 'countGroups'=>array(self::STAT, 'GroupsUsers', 'user_id'),			
			// 'videos'=>array(self::HAS_MANY, 'VideoUser', 'user_id',
			// 	'limit' => 10,
			// 	'order' => 'RAND()',
			// ),
			// 'countVideos'=>array(self::STAT, 'VideoUser', 'user_id'),
			// 'musics'=>array(self::HAS_MANY, 'MusicUser', 'user_id',
			// 	'limit' => 4,
			// 	'order' => 'musics.id DESC',
			// ),
			// 'countMusics'=>array(self::STAT, 'MusicUser', 'user_id'),		
			// 'comments'=>array(self::HAS_MANY, 'UsersComments', 'profile_id',
			// 	'limit' => 10,
			// 	'order' => 'comments.id DESC',
			// 	'condition'=>'comments.parent_id=0'
			// ),
			// 'countComments'=>array(self::STAT, 'UsersComments', 'profile_id'),
			// // Страна
			// 'country'			=>	array(self::BELONGS_TO, 'Country', 'country_id'),	
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 				=>	Yii::t('main', 'ID'),
			'last_visit' 		=>	Yii::t('main', 'Последнее посещение'),
			'username' 			=>	Yii::t('main', 'Имя пользователя'),
			'password' 			=>	Yii::t('main', 'Пароль'),
			'oldPassword' 		=>	Yii::t('main', 'Старый пароль'),
			'newPassword' 		=>	Yii::t('main', 'Новый пароль'),
			'confirmPassword'	=>	Yii::t('main', 'Повторить пароль'),
			'email'				=>	Yii::t('main', 'E-mail'),
			'oldEmail'			=>	Yii::t('main', 'Старый E-mail'),
			'newEmail'			=>	Yii::t('main', 'Новый E-mail'),
			'role' 				=>	Yii::t('main', 'Роль'),
			'identity' 			=>	Yii::t('main', 'Identity'),
			'network' 			=>	Yii::t('main', 'Network'),
			'full_name' 		=>	Yii::t('main', 'Полное имя'),
			'sex' 				=>	Yii::t('main', 'Стать'),
			'avatar' 			=>	Yii::t('main', 'Аватар'),
			'disabled' 			=>	Yii::t('main', 'Disabled'),
			'language' 			=>	Yii::t('main', 'Languages'),
			'address' 			=>	Yii::t('main', 'Адрес'),
			'phone' 			=>	Yii::t('main', 'Телефон'),
			'country_id' 		=>	Yii::t('main', 'Страна'),
			'city_id' 			=>	Yii::t('main', 'Город'),
			'birthday' 			=>	Yii::t('main', 'День рождения'),
			'status' 			=>	Yii::t('main', 'Статус'),
			'about' 			=>	Yii::t('main', 'Обо мне'),
		);
	}

	public function getUrl()
	{
		return Yii::app()->createUrl('../profile/index',array('id' => $this->id));
	}

	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password) === $this->password;
	}
	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function ganaratePassword($password)
	{
		return $this->hashPassword($password);
	}
	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	public function hashPassword($password)
	{
		return md5($password);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('last_visit',$this->last_visit,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('role',$this->role,true);
		$criteria->compare('identity',$this->identity,true);
		$criteria->compare('network',$this->network,true);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('disabled',$this->disabled);
		$criteria->compare('language',$this->language);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('country_id',$this->country_id);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('about',$this->about,true);
		return new CActiveDataProvider($this, array(
			'pagination'=>array(
					'pageSize'=> Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
			),
			'criteria'=>$criteria,
		));
	}
}